<?php

/**
 * Dat@ctiv Framework
 *
 * @author		Mathieu Rey
 * @copyright	Copyright (c) 2008, datactiv.fr
 * @link		http://www.datactiv.fr
 */

/**
 * TableauAjax
 *
 * Cette classe a pour but de simplifier l'affichage sous forme de tableau
 *
 * @author		Mathieu Rey
 */

/*
// ------------------

error_reporting("E^ALL");
set_error_handler('_exception_handler');

function _exception_handler($severity, $message, $filepath, $line){
	echo "<br /><b>".$severity." : </b> ".$message." (fichier : ".$filepath.", ligne : ".$line.")<br /><br />";
}

// ------------------
*/
class TableauAjax{

	protected $table;

	protected $icones = "default";


	function __construct(){

	}

	function setTable ($table){

		$this->table = $table;

	}

	function setIcones($set){
		$this->icones = $set;
	}

	function affiche(){

		if ($this->table == "")
			die("Oubli d'initialisation !");

	GLOBAL $URLBASE, $props, $struct;
		?>
	<link rel="stylesheet" href="<?=$URLBASE;?>css/table_widget.css" media="screen" type="text/css">
	<script type="text/javascript" src="<?=$URLBASE;?>js/dhtmlsuite/ajax.js"></script>

	<script type="text/javascript">
	DHTMLSuite.include('tableWidget');
	</script>
<div>

<table id="myTable">
	<colgroup><?php

	for ($i = 0; $i < count($props[$this->table]["cols"]) ; $i++)
		echo '<col width="'.floor(92/(count($props[$this->table]["cols"]))).'%"></col>';

	echo '<col width="8%"></col>';
?>
	</colgroup>
	<thead>
		<tr><?php

	foreach ($props[$this->table]["cols"] as $k)
		echo '<td sortBy="'.$k.'">'.$struct[$k]["nom"].'</td>';

	echo '<td sortBy=""></td>';
?>
		</tr>
	</thead>
	<tbody>

	</tbody>
</table>

<table id="myTableModel" style="display: none">
	<colgroup><?php

	for ($i = 0; $i < count($props[$this->table]["cols"]) ; $i++)
		echo '<col width="'.floor(92/count($props[$this->table]["cols"])).'%"></col>';

	echo '<col width="8%"></col>';
?>
	</colgroup>
	<thead>
		<tr><?php

	foreach ($props[$this->table]["cols"] as $k)
		echo '<td sortBy="'.$k.'">'.$struct[$k]["nom"].'</td>';

	echo '<td sortBy=""></td>';
?>
		</tr>
	</thead>
	<tbody>

	</tbody>
</table>

</div>

<div id="tableWidgetPagina" style="text-align: center; padding-top: 10px;"></div>

<?php
echo '<div style="text-decoration: none;"><a href="/'.$this->table.'/ajouter"><img src="/images/creer.gif" /> Ajouter un nouvel enregistrement</a></div>';
	?>

<script type="text/javascript">

function reinit(_columnsort,_sortfilename, _numberofrows){

	var tableWidgetObj = new DHTMLSuite.tableWidget();
	DHTMLSuite.commonObj.setCssCacheStatus(false)

	tableWidgetObj.setTableId('myTable');
	tableWidgetObj.setTableWidth('100%');
	tableWidgetObj.setTableHeight(250);
	tableWidgetObj.setServerSideSort(true);
	tableWidgetObj.setColumnSort(_columnsort);	// The value doesn't matter here since we're using server side sort. However, we need to set a value for the sortable columns.
	tableWidgetObj.setServersideSortFileName(_sortfilename);	// Specify server side file name
	tableWidgetObj.setServersideSortNumberOfRows(10);	// Receive 10 rows from the server
	tableWidgetObj.init();
	tableWidgetObj.updateTableHeader(0,'ascending');

	// Creating pagina object

	var pagehandler = new DHTMLSuite.tableWidgetPageHandler();
	pagehandler.setTargetId('tableWidgetPagina');	// Specify where to insert the paginating object.
	pagehandler.setTableRef(tableWidgetObj);	// Specify where to insert the paginating object.
	pagehandler.setMaximumNumberOfPageLinks(6);
	pagehandler.setTotalNumberOfRows(_numberofrows);
	pagehandler.init();

}

reinit(Array(<?php
	$ch = "";
		for ($i = 0; $i < count($props[$this->table]["cols"]) ; $i++)
			$ch .= "'S',";

		echo $ch."''";
	?>), '<?=$URLBASE;?>ajax/afficher/<?=$this->table;?>/<?=$this->icones; ?>', <?php
		
	$s = load("sql");
	$s->select("COUNT(*) AS nb", $this->table);
	$r = $s->fetch_object();
	echo $r->nb;

	?>);
</script>

		<?php


	}





	// #########################################################################################################################################



	

	function afficherech(){

		if ($this->table == "")
			die("Oubli d'initialisation !");

	GLOBAL $URLBASE;
		?>
	<link rel="stylesheet" href="<?=$URLBASE;?>css/table_widget.css" media="screen" type="text/css">
	<script type="text/javascript" src="<?=$URLBASE;?>js/dhtmlsuite/ajax.js"></script>

	<script type="text/javascript">
	DHTMLSuite.include('tableWidget');
	</script>
<div>
<div style="float: right; width: 550px;">
<b>Résultats de la recherche</b><br /><div>
<div id="myTableDiv"><table id="myTable">
	<colgroup>
<col width="20%"></col>
<col width="60%"></col>
<col width="20%"></col>
	</colgroup>
	<thead>
		<tr>
		<td sortBy="exo_difficulte">Difficulté</td>
		<td sortBy="exo_titre">Titre</td>
		<td sortBy=""></td>
		
		</tr>
	</thead>
	<tbody>

	</tbody>
</table></div>
</div>
</div>
<div id="myTableModelDiv"><table id="myTableModel" style="display: none">
	<colgroup>
<col width="20%"></col>
<col width="60%"></col>
<col width="20%"></col>
	</colgroup>
	<thead>
		<tr>
		<td sortBy="exo_difficulte">Difficulté</td>
		<td sortBy="exo_titre">Titre</td>
		<td sortBy=""></td>
		
		</tr>
	</thead>
	<tbody>

	</tbody>
</table></div>

</div>

<script type="text/javascript">

function reinit(_sortfilename){

	var tableWidgetObj = new DHTMLSuite.tableWidget();
	DHTMLSuite.commonObj.setCssCacheStatus(false)

	tableWidgetObj.setTableId('myTable');
	tableWidgetObj.setTableWidth(500);
	tableWidgetObj.setTableHeight(330);
	tableWidgetObj.setServerSideSort(true);
	tableWidgetObj.setColumnSort(Array('S','S',''));	// The value doesn't matter here since we're using server side sort. However, we need to set a value for the sortable columns.
	tableWidgetObj.setServersideSortFileName(_sortfilename);	// Specify server side file name
	tableWidgetObj.setServersideSortNumberOfRows(100);	// Receive 10 rows from the server
	tableWidgetObj.init();
	tableWidgetObj.sortTableByColumn(1);// Initially sort the table by the third column

}

reinit('<?=$URLBASE;?>ajax/vide');

</script>

		<?php


	}



}


?>
